/*
 * $Id: sck.h,v 1.10 2006-02-08 11:04:01 bacon Exp $
 */
 
#ifndef _XP_NET_SCK_H_
#define _XP_NET_SCK_H_

#include <xp/types.h>
#include <xp/macros.h>
#include <xp/net/socket.h>

enum
{
	XP_SCK_CONN = (1 << 0),
	XP_SCK_ACPT = (1 << 1),
	XP_SCK_SEND = (1 << 2),
	XP_SCK_RECV = (1 << 3),
	XP_SCK_ALL  = XP_SCK_CONN | XP_SCK_ACPT | XP_SCK_SEND | XP_SCK_RECV
};

typedef struct xp_sck_t xp_sck_t;

struct xp_sck_t
{
	xp_sckhnd_t sckhnd;
	xp_time_t conn_tmout;
	xp_time_t acpt_tmout;
	xp_time_t send_tmout;
	xp_time_t recv_tmout;

	xp_bool_t (*conn_cancel) (void*);
	void* conn_cancel_arg;
	
	xp_bool_t __dynamic;
};

#ifdef __cplusplus
extern "C" {
#endif

/*
 * FUNCTION: xp_sck_open
 */
xp_sck_t* xp_sck_open (xp_sck_t* sck, int domain, int type, int proto);

/*
 * FUNCTION: xp_sck_openhnd
 */
xp_sck_t* xp_sck_openhnd (xp_sck_t* sck, xp_sckhnd_t handle);

/*
 * FUNCTION: xp_sck_close
 */
int xp_sck_close (xp_sck_t* sck);

/*
 * FUNCTION: xp_sck_forfeit
 *   Destroys an sck object without closing the socket handle
 */
int xp_sck_forfeit (xp_sck_t* sck);

/*
 * FUNCTION: xp_sck_gethnd
 */
xp_sckhnd_t xp_sck_gethnd (xp_sck_t* sck);

/*
 * FUNCTION: xp_sck_shtdwn
 */
int xp_sck_shtdwn (xp_sck_t* sck, int how);

xp_time_t xp_sck_gettmout (xp_sck_t* sck, int object);
void xp_sck_settmout (xp_sck_t* sck, int object, xp_time_t tmout);

void xp_sck_setcancel (xp_sck_t* sck, 
	int object, xp_bool_t (*cancel) (void*), void* arg);

int xp_sck_conn (xp_sck_t* sck, const xp_sckadr_t* addr, xp_scklen_t len);

int xp_sck_bind (xp_sck_t* sck, xp_sckadr_t* addr, xp_scklen_t len);
int xp_sck_lstn (xp_sck_t* sck, int backlog);

xp_sck_t* xp_sck_acpt (
	xp_sck_t* sck, xp_sck_t* newsck, xp_sckadr_t* addr, xp_scklen_t* len);

xp_ssize_t xp_sck_recv (
	xp_sck_t* sck, void* buf, xp_size_t len, int flags);
xp_ssize_t xp_sck_recvall (
	xp_sck_t* sck, void* buf, xp_size_t len, int flags);

xp_ssize_t xp_sck_send (
	xp_sck_t* sck, const void* msg, xp_size_t len, int flags);
xp_ssize_t xp_sck_sendall (
	xp_sck_t* sck, const void* msg, xp_size_t len, int flags);

xp_ssize_t xp_sck_recvfrom (
	xp_sck_t* sck, void* buf, xp_size_t len, int flags,
	xp_sckadr_t* from, xp_scklen_t* fromlen);
xp_ssize_t xp_sck_sendto (
	xp_sck_t* sck, const void* msg, xp_size_t len, int flags, 
	const xp_sckadr_t* to, xp_scklen_t tolen);

int xp_sck_getopt (xp_sck_t* sck, int level,
	int optname, void* optval, xp_scklen_t* optlen);
int xp_sck_setopt (xp_sck_t* sck, int level,
	int optname, const void* optval, xp_scklen_t optlen);

int xp_sck_getself (xp_sck_t* sck, xp_sckadr_t* name, xp_scklen_t* len);
int xp_sck_getpeer (xp_sck_t* sck, xp_sckadr_t* name, xp_scklen_t* len);

#ifdef __cplusplus
}
#endif

#endif
